################################################################################
#                                                                              #
# Why Dominant Governing Parties Are Cross-Nationally Influential              #
#                                                                              #
# Tobias Boehmelt, Lawrence Ezrow, Petra Schleiter, Roni Lehrer, and Hugh Ward #
#                                                                              #
# This Version: July 12, 2017                                                  #
#                                                                              #
# Address Correspondence to: tbohmelt@essex.ac.uk                              #
#                                                                              #
################################################################################

########################################################################
# Long-Term Effects of Increase in Some Parties' Policy Positions by 1 # 
# Parameters of Full Models Used (2010)                                #
########################################################################

library(foreign)
dd <- c("C:/Users/tbohmelt/Dropbox/Party Shifts EU/7_Clarity of Responsibility Project/Analysis/ISQ Replication/Table 2/")
setwd(dd)
hu<-read.dta("Baseline.dta")

# Matrices: 
domestic<-read.dta("domestic76051.dta")[2608:2718, 2608:2718]
domfam<-read.dta("domestic_fam76051.dta")[2608:2718, 2608:2718]
foreign<-read.dta("foreign_incumb_v_share76051.dta")[2608:2718, 2608:2718]

##
mcov<-read.csv("var_covar.csv", colClasses=c(rep("numeric", 255)), header=T)
mcov<-as.matrix(mcov[1:255, ])
mb<-read.csv("betas.csv", colClasses=c(rep("numeric", 255)), header=T)
mb<-mb[1, ]

library(MASS)
Sigma <- matrix(c(mcov),ncol=length(mb), nrow=length(mb))
M<-(mvrnorm(n=1000, unlist(mb, use.names=F), Sigma))

par(mfrow=c(2,2))
plot(density(M[,4]))
plot(density(M[,1]))
plot(density(M[,2]))
plot(density(M[,3]))

phi<-mean(M[, 4])
rho.domestic<-mean(M[, 1])
rho.domfam<-mean(M[, 2])
rho.foreign<-mean(M[, 3])

S<-matrix(0, ncol=111, nrow=111)
diag(S)<-1-phi
S<-S-rho.domestic*domestic-rho.domfam*domfam-rho.foreign*foreign
S<-solve(S)

germany<-S%*%c(rep(0, 66), 1, rep(0, 44)) # this is CDU/CSU.
uk<-S%*%c(rep(0, 73), 1, rep(0, 37)) # this is Labour. 

GR100<-NULL
UK100<-NULL
for (i in 1:dim(M)[1]){
phi<-M[i, 4]
rho.domestic<-M[i, 1]
rho.fomfam<-M[i, 2]
rho.foreign<-M[i, 3]

S<-matrix(0, ncol=111, nrow=111)
diag(S)<-1-phi
S<-S-rho.domestic*domestic-rho.domfam*domfam-rho.foreign*foreign
S<-solve(S)

germany<-S%*%c(rep(0, 66), 1, rep(0, 44)) # this is CDU/CSU.
uk<-S%*%c(rep(0, 73), 1, rep(0, 37)) # this is Labour. 

GR100<-cbind(GR100, germany)
UK100<-cbind(UK100, uk)
}

##########

EE1<-GR100

Q<-NULL
for (i in 1:dim(EE1)[1]){
a<-quantile(EE1[i,], c(.05, .5, .95))
Q<-rbind(Q, a)}

(as.matrix(cbind(hu$partyname[2608:2718], Q)))

write.table(as.matrix(cbind(hu$partyname[2608:2718], Q)), file="germany.csv", sep=",",col.names=NA)

##########

EE4<-UK100

Q<-NULL
for (i in 1:dim(EE4)[1]){
a<-quantile(EE4[i,], c(.05, .5, .95))
Q<-rbind(Q, a)}

(as.matrix(cbind(hu$partyname[2608:2718], Q)))

write.table(as.matrix(cbind(hu$partyname[2608:2718], Q)), file="UK.csv", sep=",",col.names=NA)

##################################################################################################
# Output is two *.csv files: one for CDU/CSU, the other for Labour                               # 
# Table entries in Table 2 of main text focus on average estimate (50% column)                   #
# Calculations are based on simulations -- out entries may thus slightly vary from table entries #
##################################################################################################